/*
 * @LANG: java
 */
 
package com.blogspot.radialmind;

class SarTextOutputParser
{
	%%{
		machine SarTextOutputParser;

		action versions {
			sarReport.verifyAndStoreVersions();
		}
		
		action multigraphheader { 
			sarReport.prepareMultiGraphHeader();
		}
		
		action normalgraphheader { 
			sarReport.prepareNormalGraphHeader();
		}

		action dataline {
			sarReport.addDataLine();
		}
		
		action avgline {
			sarReport.addAverageLine();
		}

		# Append to the buffer.
		action append {
			sarReport.appendChar( fc );
		}
		
		# Terminate a buffer.
		action termword {
			sarReport.addWord();
		}
		
		action termdate {
			sarReport.addDate();
		}
		
		action termpmam {
			sarReport.addPmAm();
		}
		
		action termtime {
			sarReport.addTime();
		}
		
		action termanything {
			sarReport.addWord();
		}
		
		action termfloat {
			sarReport.addFloat();
		}
		
		action termint {
			sarReport.addInteger();
		}

		WS	= [ \t];
		NoNums = [ \t\(\)0-9\.\"];
		InvalidChars = [ \t\(\)\"\n];
		StringChars = [\(\)\"];

		word = (^NoNums $append) (^InvalidChars $append)* | '"' (WS $append)* (^NoNums $append) (^StringChars $append)* (WS $append)* '"';
		anything = (^InvalidChars $append)+; 
		
		numericint = '-'? $append '0' $append | '-'? $append [0-9] $append ([0-9] $append)*;
		numericfloat = ( '-'? $append) (digit $append)* '.' $append (digit $append)+ | ( '-'? $append) (digit $append)+ '.' $append;

		date = numericint '/' $append numericint '/' $append numericint;
		time = numericint ':' $append numericint ':' $append numericint; 
		version = '_' word '_'; 
		CPU = 'C' 'P' 'U';
		capitals = ([A-Z] $append)+ ;
		measure = ([a-zA-Z\%] $append) ([A-Z] $append)? (^[ \tA-Z\n] $append)+ |
			([a-z\%] $append) (^InvalidChars $append)+;

		main :=
			word %termword WS+ anything %termanything WS+ '(' WS* anything %termanything WS* ')' WS+ date %termdate WS+ version %termword WS+ '(' numericint %termint WS* CPU ')' '\n' @versions |
			time %termtime WS+ capitals %termpmam WS+ capitals %termword (WS+ measure %termword )+ '\n' @multigraphheader |
			time %termtime WS+ capitals %termpmam (WS+ measure %termword )+ '\n' @normalgraphheader |
			time %termtime WS+ capitals %termpmam WS+ anything %termword ((WS+ numericint %termint ) | (WS+ numericfloat %termfloat ))+ '\n' @dataline |
			'Average:' WS+ anything %termword ((WS+ numericint %termint ) | (WS+ numericfloat %termfloat ))+ '\n' @avgline;
	}%%

	%% write data;

	public boolean parse( GenerateSarReport sarReport, char data[] )
	{
		int cs, p = 0, pe = data.length;
		int top = 0;
		int stack[] = new int[ 16384 ];
		
		%% write init;
		%% write exec;

		if (( cs >= SarTextOutputParser_first_final ) && ( top == 0 ))
			return true;
		else
			return false;
	}
}

/* _____OUTPUT_____
ACCEPT
FAIL
FAIL
*/